//负责人：李明洋
package com.java.datamanage;
import com.java.model.Admin;
import com.java.model.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

//文件流
class FileDataManager {
    private static final String STUDENT_FILE = "students.dat";
    private static final String ADMIN_FILE = "admin.dat";

    // 保存学生数据
    public static void saveStudents(List<Student> students) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(STUDENT_FILE))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 加载学生数据
    @SuppressWarnings("unchecked")
    public static List<Student> loadStudents() {
        List<Student> students = null;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(STUDENT_FILE))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // 文件不存在或读取错误，返回空列表
        }
        return students;
    }

    // 保存管理员数据
    public static void saveAdmin(Admin admin) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(ADMIN_FILE))) {
            oos.writeObject(admin);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 加载管理员数据
    public static Admin loadAdmin() {
        Admin admin = null;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ADMIN_FILE))) {
            admin = (Admin) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // 文件不存在或读取错误，返回 null
        }
        return admin;
    }
}